package com.edu.service.impl;

import com.edu.dao.SliderDao;
import com.edu.dao.impl.SliderDaoImpl;
import com.edu.entity.SliderEntity;
import com.edu.service.SliderService;
import com.edu.utils.JwtUtils;

import java.util.List;
import java.util.Map;

/**
 * com.edu.service.impl
 * 2024/7/5 10:16
 *
 * @author Fancy
 * @version 1.0
 */
public class SliderServiceImpl implements SliderService {
    SliderDao sliderDao = new SliderDaoImpl();

    @Override
    public List<SliderEntity> getAllSlider() {
        return sliderDao.getAllSlider();
    }

    @Override
    public int updateSliderStatus(String newStatus, int sliderId, String token) {
        try {
            Map<String, Object> user = JwtUtils.validateToken(token);
            if (user.get("role").equals("admin")) {
                //管理员有权删除所有评论
                return sliderDao.updateStatus(newStatus, sliderId);
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int deleteSlider(int sliderId, String token) {
        try {
            Map<String, Object> user = JwtUtils.validateToken(token);
            if (user.get("role").equals("admin")) {
                //管理员有权删除所有评论
                return sliderDao.deleteSlider(sliderId);
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int createSlider(SliderEntity slider, String token) {
        try {
            Map<String, Object> user = JwtUtils.validateToken(token);
            if (user.get("role").equals("admin")) {
                //管理员有权删除所有评论
                return sliderDao.createSlider(slider);
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
